package com.example.jf.jfapp.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.example.jf.jfapp.R;
import com.example.jf.jfapp.activity.base.BaseActivity;
import com.example.jf.jfapp.fragment.faxian.ImageDetailFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**图片查看器
 1. 保存图片到本地
 * Created by jf on 2017/7/29.
 */

public class ImagePagerActivity extends BaseActivity {
    public static final String EXTRA_IMAGE_URLS = "image_urls";
    public static final String IMAGE_INDEX = "image_index";

    @BindView(R.id.tv_number)
    TextView tv_number;

    @BindView(R.id.vp)
    ViewPager vp;
    private List<String> urls;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_detail_pager);
        ButterKnife.bind(this);
        initView();
    }


    @Override
    public void initView() {
        urls = getIntent().getStringArrayListExtra(
                EXTRA_IMAGE_URLS);
        int index = getIntent().getIntExtra(IMAGE_INDEX,0);
        tv_number.setText((index+1)+ "/"+ urls.size());
        vp.setAdapter(new ImagePagerAdapter(getSupportFragmentManager()));
        vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                tv_number.setText((position+1)+ "/"+ urls.size());
            }
        });
    }

    private class ImagePagerAdapter extends FragmentStatePagerAdapter {
        public ImagePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return ImageDetailFragment.getInstance(urls.get(position));
        }

        @Override
        public int getCount() {
            return urls.size();
        }
    }
}
